package com.jaydenxiao.common.commonwidget;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.jaydenxiao.common.R;


/**
 * Created by admin on 2017-04-25.
 */

public class CallPhoneDialog extends AlertDialog {

    private Activity context;

    public CallPhoneDialog(Context context) {
        super(context);
        this.context = (Activity) context;
    }

    public void showDialog(final String phoneNumber) {
        //自定义布局
        View view = getLayoutInflater().inflate(R.layout.dialog_call_phone, null);
        //把自定义的布局设置到dialog中，注意，布局设置一定要在show之前。从第二个参数分别填充内容与边框之间左、上、右、下、的像素
        this.setView(view, 0, 0, 0, 0);
        //一定要先show出来再设置dialog的参数，不然就不会改变dialog的大小了
        this.show();
        //得到当前显示设备的宽度，单位是像素
        int width = context.getWindowManager().getDefaultDisplay().getWidth();
        //得到这个dialog界面的参数对象
        WindowManager.LayoutParams params = this.getWindow().getAttributes();
        //设置dialog的界面宽度
        params.width = width - (width / 6);
        //设置dialog高度为包裹内容
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置dialog的重心
        params.gravity = Gravity.CENTER;
        //dialog.getWindow().setLayout(width-(width/6), LayoutParams.WRAP_CONTENT);
        //用这个方法设置dialog大小也可以，但是这个方法不能设置重心之类的参数，推荐用Attributes设置
        //最后把这个参数对象设置进去，即与dialog绑定
        this.getWindow().setAttributes(params);
        Button callPhoneBt = (Button) view.findViewById(R.id.callPhoneBt);
        Button sendMessageBt = (Button) view.findViewById(R.id.sendMessageBt);
        Button cancelBt = (Button) view.findViewById(R.id.cancelBt);
        TextView titleTV = (TextView) view.findViewById(R.id.titleTV);
        titleTV.setText(phoneNumber);
        callPhoneBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                //打电话（跳转到拨号盘）
//                Intent intent = new Intent(Intent.ACTION_DIAL);
//                intent.setData(Uri.parse("tel:"+phoneNumber));
//                startActivity(intent);

                //打电话（直接拨打出去）
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" + phoneNumber));
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                context.startActivity(intent);
                CallPhoneDialog.this.dismiss();
            }
        });
        sendMessageBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发短信
                Intent intent = new Intent(Intent.ACTION_SENDTO);
                intent.setData(Uri.parse("smsto:"+phoneNumber));
                context.startActivity(intent);
            }
        });

        cancelBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CallPhoneDialog.this.dismiss();
            }
        });

    }







}
